import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bsbdj/bean/music_persional.dart';
import 'package:flutter_bsbdj/utils/music_play.dart';
import 'package:flutter_bsbdj/utils/widget_views.dart';

class MusicWindowPlay {
  OverlayEntry _overlayEntry;
  bool show = false;
  bool playing = false;
  String _header;
  static MusicWindowPlay windowPlay;
  BuildContext context;
  double position = 0.0;
  double duration = 1.0;
  String _pos = "00:00";
  String _dur = "00:00";
  MusicPlay _musicPlay;
  String playUrl;

  Function onPlayerStateChanged;
  Function onPlayerError;
  Function onDurationChanged;
  Function onAudioPositionChanged;
  Function onPlayerCompletion;

  static MusicWindowPlay getInstance(BuildContext context) {
    if (windowPlay == null) {
      windowPlay = MusicWindowPlay(context);
    }
    return windowPlay;
  }

  MusicWindowPlay(BuildContext cont) {
    this.context = cont;
  }

  void playMusic(String url, PersonalItem item, BuildContext context) async {
    OverlayState overlayState = Overlay.of(context);
    _header = item.pic;
    if (_overlayEntry == null) {
      _overlayEntry = OverlayEntry(
        builder: (BuildContext context) => Positioned(
              bottom: 0,
              child: AnimatedOpacity(
                child: WidgetViews.getInstance().musicPlayInfo(
                    _header,
                    position / duration,
                    _pos,
                    _dur,
                    MediaQuery.of(context).size.width,
                    60,
                    playing,
                    _stateChange,
                    context,
                    item),
                opacity: show ? 1.0 : 0.0,
                duration: show
                    ? Duration(milliseconds: 100)
                    : Duration(milliseconds: 400),
              ),
            ),
      );
      overlayState.insert(_overlayEntry);
    } else {
      _overlayEntry.markNeedsBuild();
    }
    _musicStart(url);
  }

  void _stateChange() {
    if (playing) {
      _musicPlay.pause();
    } else {
      _musicPlay.replay();
    }
    playing = !playing;
  }

  void _musicStart(String mp3) {
    if (_musicPlay == null) {
      _musicPlay = MusicPlay.getInstance().setPlayerStateChanged((state) {
        if (state == AudioPlayerState.PLAYING) {
          playing = true;
        } else {
          playing = false;
        }
        _overlayEntry.markNeedsBuild();
        if (onPlayerStateChanged != null) {
          onPlayerStateChanged(state);
        }
      }).setPlayerError((event) {
        _musicPlay.stop();
        if (onPlayerError != null) {
          playing = false;
          onPlayerError(event);
        }
      }).setDurationChanged((dur) {
//        print("+++++++++++++++++++");
//        print(dur.toString());
        duration = dur.inMicroseconds * 1.0;
        _dur = dur.toString().substring(2, 7);
        if (onDurationChanged != null) {
          onDurationChanged(dur);
        }
        _overlayEntry.markNeedsBuild();
      }).setAudioPositionChanged((pos) {
//        print("+++++++++++++++++++");
//        print(pos.toString());
        position = pos.inMicroseconds * 1.0;
        _pos = pos.toString().substring(2, 7);
        if (duration != 0) {
          _overlayEntry.markNeedsBuild();
        }
        if (onAudioPositionChanged != null) {
          onAudioPositionChanged(pos);
        }
      }).setPlayerCompletion(() {
        playing = false;
        if (onPlayerCompletion != null) {
          onPlayerCompletion();
        }
        _overlayEntry.markNeedsBuild();
      });
    }
    if (mp3 == playUrl) {
      if (_musicPlay.isPlaying) {
//        playing=false;
//        _musicPlay.pause();
      } else {
        playing = true;
        _musicPlay.playMusic(mp3, false);
      }
    } else {
      playUrl = mp3;
      _musicPlay.pause();
      _musicPlay.stop();
      playing = true;
      _musicPlay.playMusic(mp3, false);
    }
  }

  void seekTo(int position) {
    _musicPlay.seekTo(position);
  }

  void isHeidden(bool isShow) {
    show = !isShow;
    if (_overlayEntry != null) {
      _overlayEntry.markNeedsBuild();
    }
  }

  bool musicIsPlayShow() {
    return show;
  }

  bool musicIsPlaying() {
    return playing;
  }

  AudioPlayerState getPlayState() {
    return _musicPlay.getPlayState();
  }

  void pause() {
    _musicPlay.pause();
  }

  void rePlay() {
    _musicPlay.replay();
  }

  void _stopPlay() {}

  void distory(){
    isHeidden(true);
    pause();
    if(_overlayEntry!=null){
      _overlayEntry.remove();
      _overlayEntry=null;
    }
  }

  void musicPlayInfoListener(
      Function playerStateChanged,
      Function playerError,
      Function durationChanged,
      Function audioPositionChanged,
      Function playerCompletion) {
    onPlayerStateChanged = playerStateChanged;
    onPlayerError = playerError;
    onDurationChanged = durationChanged;
    onAudioPositionChanged = audioPositionChanged;
    onPlayerCompletion = playerCompletion;
  }

  double _durationToDouble(String str) {
    String d = str.substring(0, 7);
    d.split(":");
  }
}
